package com.example.communist_app_api.service.impl;

import com.example.communist_app_api.config.mq.NoticeTask;
import com.example.communist_app_api.db.dao.UserDao;
import com.example.communist_app_api.db.dao.WordDao;
import com.example.communist_app_api.db.dto.WordDTO;
<<<<<<< HEAD
import com.example.communist_app_api.db.pojo.User;
import com.example.communist_app_api.db.pojo.Word;
import com.example.communist_app_api.exception.GlobalException;
=======
import com.example.communist_app_api.db.pojoDto.User;
import com.example.communist_app_api.db.pojoDto.Word;
>>>>>>> 党建uniapp/dev
import com.example.communist_app_api.sensitive.MyDdWordAllow;
import com.example.communist_app_api.sensitive.MyDdWordDeny;
import com.example.communist_app_api.service.WordService;
import com.example.communist_app_api.vo.web.resp.SensitiveWordApiResp;
import com.github.houbb.sensitive.word.bs.SensitiveWordBs;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author Brother Sheng Editor
 * @version 1.0
 * Create by 2023/2/24 16:03
 */
@Service
@Transactional
public class WordServiceImpl implements WordService {
    @Autowired
    private WordDao wordDao;

    @Autowired
    private UserDao userDao;

    @Autowired
    private MyDdWordAllow myDdWordAllow;

    @Autowired
    private MyDdWordDeny myDdWordDeny;

    @Autowired
    private NoticeTask noticeTask;

    //从数据库中寻找 状态status为正常，类型type是允许的单词
    @Override
    public List<String> selectAllWordByNormalAndAllow() {
        return wordDao.selectAllWordByNormalAndAllow();
    }

    //从数据库中寻找 状态status为正常，类型type是禁止的单词
    @Override
    public List<String> selectAllWordByNormalAndDeny() {
        return wordDao.selectAllWordByNormalAndDeny();
    }

    //新增word对象
    @Override
    public Integer insert(Word wordEntity) {
        return wordDao.insert(wordEntity);
    }

    @Override
    public Integer delete(Integer id) {
        return wordDao.deleteByPrimaryKey(id);
    }

    @Override
    public Integer update(Word wordEntity) {
        return wordDao.updateByPrimaryKeySelective(wordEntity);
    }

    @Override
    public PageInfo<Word>  selectByCondition(Word wordEntity,Integer pageNum ,Integer pageSize) {
        Page<Word> page=new Page<>(pageNum,pageSize);
        List<Word> wordsList = wordDao.selectByCondition(wordEntity);
        //获取所有操作员并去重
        List<Integer> operatorIdList = wordsList.stream()
                .map(word ->Integer.valueOf(word.getOperatorId()))
                .distinct()
                .collect(Collectors.toList());
        if(operatorIdList.size() == 0){
            return new PageInfo<>(wordsList,5);
        }
        //实例化一个HashMap存储<id,name>
        Map<Integer, User> map = userDao.getUserListByIds(operatorIdList).stream()
                .collect(Collectors.toMap(User::getId, user -> user));
        //遍历List
        wordsList.forEach(word -> {
            User user = map.get(Integer.valueOf(word.getOperatorId()));
            word.setOperatorName(user.getName());
        });

        page.setTotal(wordsList.size());
        Integer skip= (pageNum-1)*pageSize;
        wordsList= wordsList.stream().skip(skip)
                .limit(pageSize).collect(Collectors.toList());
        page.addAll(wordsList);

        return new PageInfo<>(page);
    }

    @Override
    public Integer batchInsert(List<WordDTO> wordInsertList) {
        return wordDao.batchInsert(wordInsertList);
    }

    //receiverId传的是小程序用户ID
    //receiverId传的是小程序用户ID，返回值判断为false时已经向用户发送消息，不用存数据库;判断成功true才可以存数据库
    @Override
    public SensitiveWordApiResp detectSensitiveWord(String text,String receiverId) {

        SensitiveWordBs wordBs = SensitiveWordBs.newInstance()
                .ignoreCase(true)
                .ignoreWidth(true)
                .ignoreNumStyle(true)
                .ignoreChineseStyle(true)
                .ignoreEnglishStyle(true)
                .ignoreRepeat(false)
                .enableNumCheck(true)
                .enableEmailCheck(true)
                .enableUrlCheck(true)
                .numCheckLen(8)
                //使用自定义词库
                .wordDeny(myDdWordDeny)
                .wordAllow(myDdWordAllow);

        //判断是否含有敏感词
        boolean contains = wordBs.contains(text);

        List<String> allSensitive = wordBs.findAll(text);
        //java8新特性，先将集合转换成流的形式，在转换成字符串
        String sensitiveWordByString = String.join(",", allSensitive);

        //实例一个返回Resp对象
        SensitiveWordApiResp resp = new SensitiveWordApiResp();
        resp.setIsSensitiveWord(contains);

        //true为有敏感词 false为符合
        if (contains) {
            String noticeName = "不符合平台规定评论";
            String msg = "您所发的言论可能含有敏感词，如下: "+sensitiveWordByString+" 麻烦请您稍作修改后再发表！谢谢您的配合！";
            //有敏感词发送消息
            noticeTask.sendNotice(0,receiverId,noticeName,msg);
        }
        return resp;
    }
}
